package com.a4studio.android.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;
import android.util.Log;

public class NewsPersistence {
	
	private final static String TAG = NewsPersistence.class.getSimpleName();
	
	private File sdcardStorage;
	private String sdcardPath;
	private String newsPath;
	
	private final static String PACKAGE_NAME = "news_wmu"; 
	
	private final static String fileExtensionName = ".wdata";	
	private int fileNum = 0;
	
	public NewsPersistence() throws Exception
	{
		
		if(Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_REMOVED))
		{
			throw new Exception("no external storage like sdcard");
		}
		
		sdcardStorage = Environment.getExternalStorageDirectory();
		sdcardPath = sdcardStorage.getParent() + java.io.File.separator + sdcardStorage.getName();
		newsPath = sdcardPath+java.io.File.separator + PACKAGE_NAME;
		
		File newsFile = new File(newsPath);
		
		if(!newsFile.exists())
		{
			newsFile.mkdir();
		}
	}
	
	public synchronized List<String> getFileList()
	{
		List<String> files = new ArrayList<String>();
		
		File dir = new File(newsPath);	
		
		for(File file : dir.listFiles())
		{
			if(file.isFile() && file.getName().endsWith(fileExtensionName)){
				files.add(file.getName());	
			}
			
		}
		
		return files;
	}

	public synchronized void storeNewsItemList(List<NewsItem> items) throws IOException
	{
		for(NewsItem item : items)
		{
			fileNum++;
			long ticket = System.currentTimeMillis();
			String filename = newsPath+java.io.File.separator + ticket+fileNum+fileExtensionName;
			File newfile = new File(filename);
			newfile.createNewFile();
				
			
			ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(newfile));
			objOutput.writeObject(item);
		}

	}
	
	public synchronized void storeNewsItem(NewsItem item) throws IOException
	{
		Log.d(TAG, "storeNewsItem"+item.getContent());
		fileNum++;
		long ticket = System.currentTimeMillis();
		String filename = newsPath + java.io.File.separator + ticket + fileNum
				+ fileExtensionName;
		File newfile = new File(filename);
		newfile.createNewFile();
		ObjectOutputStream objOutput = new ObjectOutputStream(
				new FileOutputStream(newfile));
		objOutput.writeObject(item);

	}
	
	public synchronized List<NewsItem> retrieveNewsItem() throws Exception
	{  
		File dir = new File(newsPath);
		List<NewsItem> items = new ArrayList<NewsItem>();
		for(File file : dir.listFiles())
		{
			if(file.isFile() && file.getName().endsWith(fileExtensionName))
			{
				ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
				NewsItem item = (NewsItem)input.readObject();
				Log.d(TAG, "retrieveNewsItem"+file.getName());
				items.add(item);
			}

		}
		
		return items;
	}         
	
	public synchronized void removeAllNewsItems() 
	{
		File dir = new File(newsPath);
		
		for(File file : dir.listFiles())
		{
			if(!file.delete())
			{
				Log.e(TAG, "delete file "+file.getName() + "delete fail");
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
